<html>
  <head>
  <title>graphicsGridworldDisplay.py</title>
  </head>
  <body>
  <h3>graphicsGridworldDisplay.py (<a href="../graphicsGridworldDisplay.py">original</a>)</h3>
  <hr>
  <pre>
<span style="color: green; font-style: italic"># graphicsGridworldDisplay.py
# ---------------------------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html

</span><span style="color: blue; font-weight: bold">import </span>util
<span style="color: blue; font-weight: bold">from </span>graphicsUtils <span style="color: blue; font-weight: bold">import </span><span style="font-weight: bold">*

</span><span style="color: blue; font-weight: bold">class </span>GraphicsGridworldDisplay<span style="font-weight: bold">:
  
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gridworld<span style="font-weight: bold">, </span>size<span style="font-weight: bold">=</span><span style="color: red">120</span><span style="font-weight: bold">, </span>speed<span style="font-weight: bold">=</span><span style="color: red">1.0</span><span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld <span style="font-weight: bold">= </span>gridworld
    <span style="color: blue">self</span><span style="font-weight: bold">.</span>size <span style="font-weight: bold">= </span>size
    <span style="color: blue">self</span><span style="font-weight: bold">.</span>speed <span style="font-weight: bold">= </span>speed
    
  <span style="color: blue; font-weight: bold">def </span>start<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span>setup<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">, </span>size<span style="font-weight: bold">=</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>size<span style="font-weight: bold">)
  
  </span><span style="color: blue; font-weight: bold">def </span>pause<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span>wait_for_keys<span style="font-weight: bold">()
  
  </span><span style="color: blue; font-weight: bold">def </span>displayValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>agent<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: red">'Agent Values'</span><span style="font-weight: bold">):
    </span>values <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">()
    </span>policy <span style="font-weight: bold">= {}
    </span>states <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>states<span style="font-weight: bold">:
      </span>values<span style="font-weight: bold">[</span>state<span style="font-weight: bold">] = </span>agent<span style="font-weight: bold">.</span>getValue<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
      </span>policy<span style="font-weight: bold">[</span>state<span style="font-weight: bold">] = </span>agent<span style="font-weight: bold">.</span>getPolicy<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
    </span>drawValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">, </span>values<span style="font-weight: bold">, </span>policy<span style="font-weight: bold">, </span>currentState<span style="font-weight: bold">, </span>message<span style="font-weight: bold">)
    </span>sleep<span style="font-weight: bold">(</span><span style="color: red">0.05 </span><span style="font-weight: bold">/ </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>speed<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>displayNullValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: red">''</span><span style="font-weight: bold">):
    </span>values <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">()
    </span><span style="color: green; font-style: italic">#policy = {}
    </span>states <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>states<span style="font-weight: bold">:
      </span>values<span style="font-weight: bold">[</span>state<span style="font-weight: bold">] = </span><span style="color: red">0.0
      </span><span style="color: green; font-style: italic">#policy[state] = agent.getPolicy(state)
    </span>drawNullValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">, </span>currentState<span style="font-weight: bold">,</span><span style="color: red">''</span><span style="font-weight: bold">)
    </span><span style="color: green; font-style: italic"># drawValues(self.gridworld, values, policy, currentState, message)
    </span>sleep<span style="font-weight: bold">(</span><span style="color: red">0.05 </span><span style="font-weight: bold">/ </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>speed<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>displayQValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>agent<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: red">'Agent Q-Values'</span><span style="font-weight: bold">):
    </span>qValues <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">()
    </span>states <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>states<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">):
        </span>qValues<span style="font-weight: bold">[(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)] = </span>agent<span style="font-weight: bold">.</span>getQValue<span style="font-weight: bold">(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)
    </span>drawQValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">, </span>qValues<span style="font-weight: bold">, </span>currentState<span style="font-weight: bold">, </span>message<span style="font-weight: bold">)
    </span>sleep<span style="font-weight: bold">(</span><span style="color: red">0.05 </span><span style="font-weight: bold">/ </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>speed<span style="font-weight: bold">)

</span>BACKGROUND_COLOR <span style="font-weight: bold">= </span>formatColor<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">)    
</span>EDGE_COLOR <span style="font-weight: bold">= </span>formatColor<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)
</span>OBSTACLE_COLOR <span style="font-weight: bold">= </span>formatColor<span style="font-weight: bold">(</span><span style="color: red">0.5</span><span style="font-weight: bold">,</span><span style="color: red">0.5</span><span style="font-weight: bold">,</span><span style="color: red">0.5</span><span style="font-weight: bold">)
</span>TEXT_COLOR <span style="font-weight: bold">= </span>formatColor<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)
</span>MUTED_TEXT_COLOR <span style="font-weight: bold">= </span>formatColor<span style="font-weight: bold">(</span><span style="color: red">0.7</span><span style="font-weight: bold">,</span><span style="color: red">0.7</span><span style="font-weight: bold">,</span><span style="color: red">0.7</span><span style="font-weight: bold">)
</span>LOCATION_COLOR <span style="font-weight: bold">= </span>formatColor<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">1</span><span style="font-weight: bold">)

</span>WINDOW_SIZE <span style="font-weight: bold">= -</span><span style="color: red">1
</span>GRID_SIZE <span style="font-weight: bold">= -</span><span style="color: red">1
</span>GRID_HEIGHT <span style="font-weight: bold">= -</span><span style="color: red">1
</span>MARGIN <span style="font-weight: bold">= -</span><span style="color: red">1

</span><span style="color: blue; font-weight: bold">def </span>setup<span style="font-weight: bold">(</span>gridworld<span style="font-weight: bold">, </span>title <span style="font-weight: bold">= </span><span style="color: red">"Gridworld Display"</span><span style="font-weight: bold">, </span>size <span style="font-weight: bold">= </span><span style="color: red">120</span><span style="font-weight: bold">):
  </span><span style="color: blue; font-weight: bold">global </span>GRID_SIZE<span style="font-weight: bold">, </span>MARGIN<span style="font-weight: bold">, </span>SCREEN_WIDTH<span style="font-weight: bold">, </span>SCREEN_HEIGHT<span style="font-weight: bold">, </span>GRID_HEIGHT
  grid <span style="font-weight: bold">= </span>gridworld<span style="font-weight: bold">.</span>grid
  WINDOW_SIZE <span style="font-weight: bold">= </span>size
  GRID_SIZE <span style="font-weight: bold">= </span>size
  GRID_HEIGHT <span style="font-weight: bold">= </span>grid<span style="font-weight: bold">.</span>height
  MARGIN <span style="font-weight: bold">= </span>GRID_SIZE <span style="font-weight: bold">* </span><span style="color: red">0.75
  </span>screen_width <span style="font-weight: bold">= (</span>grid<span style="font-weight: bold">.</span>width <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">) * </span>GRID_SIZE <span style="font-weight: bold">+ </span>MARGIN <span style="font-weight: bold">* </span><span style="color: red">2
  </span>screen_height <span style="font-weight: bold">= (</span>grid<span style="font-weight: bold">.</span>height <span style="font-weight: bold">- </span><span style="color: red">0.5</span><span style="font-weight: bold">) * </span>GRID_SIZE <span style="font-weight: bold">+ </span>MARGIN <span style="font-weight: bold">* </span><span style="color: red">2

  </span>begin_graphics<span style="font-weight: bold">(</span>screen_width<span style="font-weight: bold">,    
                 </span>screen_height<span style="font-weight: bold">,
                 </span>BACKGROUND_COLOR<span style="font-weight: bold">, </span>title<span style="font-weight: bold">=</span>title<span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>drawNullValues<span style="font-weight: bold">(</span>gridworld<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: red">''</span><span style="font-weight: bold">):
  </span>grid <span style="font-weight: bold">= </span>gridworld<span style="font-weight: bold">.</span>grid
  blank<span style="font-weight: bold">()
  </span><span style="color: blue; font-weight: bold">for </span>x <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>width<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">for </span>y <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>height<span style="font-weight: bold">):
      </span>state <span style="font-weight: bold">= (</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">)
      </span>gridType <span style="font-weight: bold">= </span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">]
      </span>isExit <span style="font-weight: bold">= (</span>str<span style="font-weight: bold">(</span>gridType<span style="font-weight: bold">) != </span>gridType<span style="font-weight: bold">)
      </span>isCurrent <span style="font-weight: bold">= (</span>currentState <span style="font-weight: bold">== </span>state<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">if </span>gridType <span style="font-weight: bold">== </span><span style="color: red">'#'</span><span style="font-weight: bold">:
        </span>drawSquare<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: blue">None</span><span style="font-weight: bold">, </span><span style="color: blue">None</span><span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
        </span>drawNullSquare<span style="font-weight: bold">(</span>gridworld<span style="font-weight: bold">.</span>grid<span style="font-weight: bold">, </span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>isExit<span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">)
  </span>pos <span style="font-weight: bold">= </span>to_screen<span style="font-weight: bold">(((</span>grid<span style="font-weight: bold">.</span>width <span style="font-weight: bold">- </span><span style="color: red">1.0</span><span style="font-weight: bold">) / </span><span style="color: red">2.0</span><span style="font-weight: bold">, - </span><span style="color: red">0.8</span><span style="font-weight: bold">))
  </span>text<span style="font-weight: bold">( </span>pos<span style="font-weight: bold">, </span>TEXT_COLOR<span style="font-weight: bold">, </span>message<span style="font-weight: bold">, </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, -</span><span style="color: red">32</span><span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"c"</span><span style="font-weight: bold">)
  

</span><span style="color: blue; font-weight: bold">def </span>drawValues<span style="font-weight: bold">(</span>gridworld<span style="font-weight: bold">, </span>values<span style="font-weight: bold">, </span>policy<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: red">'State Values'</span><span style="font-weight: bold">):
  </span>grid <span style="font-weight: bold">= </span>gridworld<span style="font-weight: bold">.</span>grid
  blank<span style="font-weight: bold">()
  </span>valueList <span style="font-weight: bold">= [</span>values<span style="font-weight: bold">[</span>state<span style="font-weight: bold">] </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>gridworld<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()] + [</span><span style="color: red">0.0</span><span style="font-weight: bold">]
  </span>minValue <span style="font-weight: bold">= </span>min<span style="font-weight: bold">(</span>valueList<span style="font-weight: bold">)
  </span>maxValue <span style="font-weight: bold">= </span>max<span style="font-weight: bold">(</span>valueList<span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">for </span>x <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>width<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">for </span>y <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>height<span style="font-weight: bold">):
      </span>state <span style="font-weight: bold">= (</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">)
      </span>gridType <span style="font-weight: bold">= </span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">]
      </span>isExit <span style="font-weight: bold">= (</span>str<span style="font-weight: bold">(</span>gridType<span style="font-weight: bold">) != </span>gridType<span style="font-weight: bold">)
      </span>isCurrent <span style="font-weight: bold">= (</span>currentState <span style="font-weight: bold">== </span>state<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">if </span>gridType <span style="font-weight: bold">== </span><span style="color: red">'#'</span><span style="font-weight: bold">:
        </span>drawSquare<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: blue">None</span><span style="font-weight: bold">, </span><span style="color: blue">None</span><span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
        </span>value <span style="font-weight: bold">= </span>values<span style="font-weight: bold">[</span>state<span style="font-weight: bold">]
        </span>action <span style="font-weight: bold">= </span><span style="color: blue">None
        </span><span style="color: blue; font-weight: bold">if </span>policy <span style="font-weight: bold">!= </span><span style="color: blue">None </span><span style="color: blue; font-weight: bold">and </span>state <span style="color: blue; font-weight: bold">in </span>policy<span style="font-weight: bold">:
          </span>action <span style="font-weight: bold">= </span>policy<span style="font-weight: bold">[</span>state<span style="font-weight: bold">]
          </span>actions <span style="font-weight: bold">= </span>gridworld<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
        </span><span style="color: blue; font-weight: bold">if </span>action <span style="color: blue; font-weight: bold">not in </span>actions <span style="color: blue; font-weight: bold">and </span><span style="color: red">'exit' </span><span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">:
          </span>action <span style="font-weight: bold">= </span><span style="color: red">'exit'
        </span>valString <span style="font-weight: bold">= </span><span style="color: red">'%.2f' </span><span style="font-weight: bold">% </span>value
        drawSquare<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span>value<span style="font-weight: bold">, </span>minValue<span style="font-weight: bold">, </span>maxValue<span style="font-weight: bold">, </span>valString<span style="font-weight: bold">, </span>action<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>isExit<span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">)
  </span>pos <span style="font-weight: bold">= </span>to_screen<span style="font-weight: bold">(((</span>grid<span style="font-weight: bold">.</span>width <span style="font-weight: bold">- </span><span style="color: red">1.0</span><span style="font-weight: bold">) / </span><span style="color: red">2.0</span><span style="font-weight: bold">, - </span><span style="color: red">0.8</span><span style="font-weight: bold">))
  </span>text<span style="font-weight: bold">( </span>pos<span style="font-weight: bold">, </span>TEXT_COLOR<span style="font-weight: bold">, </span>message<span style="font-weight: bold">, </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, -</span><span style="color: red">32</span><span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"c"</span><span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>drawQValues<span style="font-weight: bold">(</span>gridworld<span style="font-weight: bold">, </span>qValues<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: red">'State-Action Q-Values'</span><span style="font-weight: bold">):
  </span>grid <span style="font-weight: bold">= </span>gridworld<span style="font-weight: bold">.</span>grid
  blank<span style="font-weight: bold">()
  </span>stateCrossActions <span style="font-weight: bold">= [[(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>gridworld<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)] </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>gridworld<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()]
  </span>qStates <span style="font-weight: bold">= </span>reduce<span style="font-weight: bold">(</span><span style="color: blue; font-weight: bold">lambda </span>x<span style="font-weight: bold">,</span>y<span style="font-weight: bold">: </span>x<span style="font-weight: bold">+</span>y<span style="font-weight: bold">, </span>stateCrossActions<span style="font-weight: bold">, [])
  </span>qValueList <span style="font-weight: bold">= [</span>qValues<span style="font-weight: bold">[(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)] </span><span style="color: blue; font-weight: bold">for </span>state<span style="font-weight: bold">, </span>action <span style="color: blue; font-weight: bold">in </span>qStates<span style="font-weight: bold">] + [</span><span style="color: red">0.0</span><span style="font-weight: bold">]
  </span>minValue <span style="font-weight: bold">= </span>min<span style="font-weight: bold">(</span>qValueList<span style="font-weight: bold">)
  </span>maxValue <span style="font-weight: bold">= </span>max<span style="font-weight: bold">(</span>qValueList<span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">for </span>x <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>width<span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">for </span>y <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>height<span style="font-weight: bold">):
      </span>state <span style="font-weight: bold">= (</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">)
      </span>gridType <span style="font-weight: bold">= </span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">]
      </span>isExit <span style="font-weight: bold">= (</span>str<span style="font-weight: bold">(</span>gridType<span style="font-weight: bold">) != </span>gridType<span style="font-weight: bold">)
      </span>isCurrent <span style="font-weight: bold">= (</span>currentState <span style="font-weight: bold">== </span>state<span style="font-weight: bold">)
      </span>actions <span style="font-weight: bold">= </span>gridworld<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">if </span>actions <span style="font-weight: bold">== </span><span style="color: blue">None </span><span style="color: blue; font-weight: bold">or </span>len<span style="font-weight: bold">(</span>actions<span style="font-weight: bold">) == </span><span style="color: red">0</span><span style="font-weight: bold">:
        </span>actions <span style="font-weight: bold">= [</span><span style="color: blue">None</span><span style="font-weight: bold">]
      </span>bestQ <span style="font-weight: bold">= </span>max<span style="font-weight: bold">([</span>qValues<span style="font-weight: bold">[(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)] </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">])
      </span>bestActions <span style="font-weight: bold">= [</span>action <span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>actions <span style="color: blue; font-weight: bold">if </span>qValues<span style="font-weight: bold">[(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)] == </span>bestQ<span style="font-weight: bold">]

      </span>q <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">()
      </span>valStrings <span style="font-weight: bold">= {}
      </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">:
        </span>v <span style="font-weight: bold">= </span>qValues<span style="font-weight: bold">[(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)]
        </span>q<span style="font-weight: bold">[</span>action<span style="font-weight: bold">] += </span>v
        valStrings<span style="font-weight: bold">[</span>action<span style="font-weight: bold">] = </span><span style="color: red">'%.2f' </span><span style="font-weight: bold">% </span>v
      <span style="color: blue; font-weight: bold">if </span>gridType <span style="font-weight: bold">== </span><span style="color: red">'#'</span><span style="font-weight: bold">:
        </span>drawSquare<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">, </span><span style="color: blue">None</span><span style="font-weight: bold">, </span><span style="color: blue">None</span><span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">elif </span>isExit<span style="font-weight: bold">:
        </span>action <span style="font-weight: bold">= </span><span style="color: red">'exit'
        </span>value <span style="font-weight: bold">= </span>q<span style="font-weight: bold">[</span>action<span style="font-weight: bold">]
        </span>valString <span style="font-weight: bold">= </span><span style="color: red">'%.2f' </span><span style="font-weight: bold">% </span>value
        drawSquare<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span>value<span style="font-weight: bold">, </span>minValue<span style="font-weight: bold">, </span>maxValue<span style="font-weight: bold">, </span>valString<span style="font-weight: bold">, </span>action<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>isExit<span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
        </span>drawSquareQ<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span>q<span style="font-weight: bold">, </span>minValue<span style="font-weight: bold">, </span>maxValue<span style="font-weight: bold">, </span>valStrings<span style="font-weight: bold">, </span>actions<span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">)
  </span>pos <span style="font-weight: bold">= </span>to_screen<span style="font-weight: bold">(((</span>grid<span style="font-weight: bold">.</span>width <span style="font-weight: bold">- </span><span style="color: red">1.0</span><span style="font-weight: bold">) / </span><span style="color: red">2.0</span><span style="font-weight: bold">, - </span><span style="color: red">0.8</span><span style="font-weight: bold">))
  </span>text<span style="font-weight: bold">( </span>pos<span style="font-weight: bold">, </span>TEXT_COLOR<span style="font-weight: bold">, </span>message<span style="font-weight: bold">, </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, -</span><span style="color: red">32</span><span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"c"</span><span style="font-weight: bold">)


</span><span style="color: blue; font-weight: bold">def </span>blank<span style="font-weight: bold">():
  </span>clear_screen<span style="font-weight: bold">()

</span><span style="color: blue; font-weight: bold">def </span>drawNullSquare<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">,</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span>isObstacle<span style="font-weight: bold">, </span>isTerminal<span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">):      

  </span>square_color <span style="font-weight: bold">= </span>getColor<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">, -</span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
  
  </span><span style="color: blue; font-weight: bold">if </span>isObstacle<span style="font-weight: bold">:
    </span>square_color <span style="font-weight: bold">= </span>OBSTACLE_COLOR
    
  <span style="font-weight: bold">(</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">) = </span>to_screen<span style="font-weight: bold">((</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">))
  </span>square<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), 
                 </span><span style="color: red">0.5</span><span style="font-weight: bold">* </span>GRID_SIZE<span style="font-weight: bold">, 
                 </span>color <span style="font-weight: bold">= </span>square_color<span style="font-weight: bold">,
                 </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">,
                 </span>width <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">)
  
  </span>square<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), 
                 </span><span style="color: red">0.5</span><span style="font-weight: bold">* </span>GRID_SIZE<span style="font-weight: bold">, 
                 </span>color <span style="font-weight: bold">= </span>EDGE_COLOR<span style="font-weight: bold">,
                 </span>filled <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">,
                 </span>width <span style="font-weight: bold">= </span><span style="color: red">3</span><span style="font-weight: bold">)
  
  </span><span style="color: blue; font-weight: bold">if </span>isTerminal <span style="color: blue; font-weight: bold">and not </span>isObstacle<span style="font-weight: bold">:
    </span>square<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), 
                 </span><span style="color: red">0.4</span><span style="font-weight: bold">* </span>GRID_SIZE<span style="font-weight: bold">, 
                 </span>color <span style="font-weight: bold">= </span>EDGE_COLOR<span style="font-weight: bold">,
                 </span>filled <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">,
                 </span>width <span style="font-weight: bold">= </span><span style="color: red">2</span><span style="font-weight: bold">)
    </span>text<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), 
           </span>TEXT_COLOR<span style="font-weight: bold">, 
           </span>str<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">]), 
           </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, -</span><span style="color: red">24</span><span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"c"</span><span style="font-weight: bold">)
      
  
  </span>text_color <span style="font-weight: bold">= </span>TEXT_COLOR

  <span style="color: blue; font-weight: bold">if not </span>isObstacle <span style="color: blue; font-weight: bold">and </span>isCurrent<span style="font-weight: bold">:
    </span>circle<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), </span><span style="color: red">0.1</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>LOCATION_COLOR<span style="font-weight: bold">, </span>fillColor<span style="font-weight: bold">=</span>LOCATION_COLOR <span style="font-weight: bold">)

  </span><span style="color: green; font-style: italic"># if not isObstacle:
  #   text( (screen_x, screen_y), text_color, valStr, "Courier", 24, "bold", "c")
      
</span><span style="color: blue; font-weight: bold">def </span>drawSquare<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span>val<span style="font-weight: bold">, </span>min<span style="font-weight: bold">, </span>max<span style="font-weight: bold">, </span>valStr<span style="font-weight: bold">, </span>action<span style="font-weight: bold">, </span>isObstacle<span style="font-weight: bold">, </span>isTerminal<span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">):

  </span>square_color <span style="font-weight: bold">= </span>getColor<span style="font-weight: bold">(</span>val<span style="font-weight: bold">, </span>min<span style="font-weight: bold">, </span>max<span style="font-weight: bold">)
  
  </span><span style="color: blue; font-weight: bold">if </span>isObstacle<span style="font-weight: bold">:
    </span>square_color <span style="font-weight: bold">= </span>OBSTACLE_COLOR
    
  <span style="font-weight: bold">(</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">) = </span>to_screen<span style="font-weight: bold">((</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">))
  </span>square<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), 
                 </span><span style="color: red">0.5</span><span style="font-weight: bold">* </span>GRID_SIZE<span style="font-weight: bold">, 
                 </span>color <span style="font-weight: bold">= </span>square_color<span style="font-weight: bold">,
                 </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">,
                 </span>width <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">)
  </span>square<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), 
                 </span><span style="color: red">0.5</span><span style="font-weight: bold">* </span>GRID_SIZE<span style="font-weight: bold">, 
                 </span>color <span style="font-weight: bold">= </span>EDGE_COLOR<span style="font-weight: bold">,
                 </span>filled <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">,
                 </span>width <span style="font-weight: bold">= </span><span style="color: red">3</span><span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">if </span>isTerminal <span style="color: blue; font-weight: bold">and not </span>isObstacle<span style="font-weight: bold">:
    </span>square<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), 
                 </span><span style="color: red">0.4</span><span style="font-weight: bold">* </span>GRID_SIZE<span style="font-weight: bold">, 
                 </span>color <span style="font-weight: bold">= </span>EDGE_COLOR<span style="font-weight: bold">,
                 </span>filled <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">,
                 </span>width <span style="font-weight: bold">= </span><span style="color: red">2</span><span style="font-weight: bold">)
        
    
  </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'north'</span><span style="font-weight: bold">:
    </span>polygon<span style="font-weight: bold">( [(</span>screen_x<span style="font-weight: bold">, </span>screen_y <span style="font-weight: bold">- </span><span style="color: red">0.45</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">), (</span>screen_x<span style="font-weight: bold">+</span><span style="color: red">0.05</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">-</span><span style="color: red">0.40</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">), (</span>screen_x<span style="font-weight: bold">-</span><span style="color: red">0.05</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">-</span><span style="color: red">0.40</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">)], </span>EDGE_COLOR<span style="font-weight: bold">, </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'south'</span><span style="font-weight: bold">:
    </span>polygon<span style="font-weight: bold">( [(</span>screen_x<span style="font-weight: bold">, </span>screen_y <span style="font-weight: bold">+ </span><span style="color: red">0.45</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">), (</span>screen_x<span style="font-weight: bold">+</span><span style="color: red">0.05</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">+</span><span style="color: red">0.40</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">), (</span>screen_x<span style="font-weight: bold">-</span><span style="color: red">0.05</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">+</span><span style="color: red">0.40</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">)], </span>EDGE_COLOR<span style="font-weight: bold">, </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'west'</span><span style="font-weight: bold">:
    </span>polygon<span style="font-weight: bold">( [(</span>screen_x<span style="font-weight: bold">-</span><span style="color: red">0.45</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), (</span>screen_x<span style="font-weight: bold">-</span><span style="color: red">0.4</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">+</span><span style="color: red">0.05</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">), (</span>screen_x<span style="font-weight: bold">-</span><span style="color: red">0.4</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">-</span><span style="color: red">0.05</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">)], </span>EDGE_COLOR<span style="font-weight: bold">, </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'east'</span><span style="font-weight: bold">:
    </span>polygon<span style="font-weight: bold">( [(</span>screen_x<span style="font-weight: bold">+</span><span style="color: red">0.45</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), (</span>screen_x<span style="font-weight: bold">+</span><span style="color: red">0.4</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">+</span><span style="color: red">0.05</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">), (</span>screen_x<span style="font-weight: bold">+</span><span style="color: red">0.4</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">-</span><span style="color: red">0.05</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">)], </span>EDGE_COLOR<span style="font-weight: bold">, </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
    
  
  </span>text_color <span style="font-weight: bold">= </span>TEXT_COLOR

  <span style="color: blue; font-weight: bold">if not </span>isObstacle <span style="color: blue; font-weight: bold">and </span>isCurrent<span style="font-weight: bold">:
    </span>circle<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), </span><span style="color: red">0.1</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>outlineColor<span style="font-weight: bold">=</span>LOCATION_COLOR<span style="font-weight: bold">, </span>fillColor<span style="font-weight: bold">=</span>LOCATION_COLOR <span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">if not </span>isObstacle<span style="font-weight: bold">:
    </span>text<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), </span>text_color<span style="font-weight: bold">, </span>valStr<span style="font-weight: bold">, </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, -</span><span style="color: red">30</span><span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"c"</span><span style="font-weight: bold">)


</span><span style="color: blue; font-weight: bold">def </span>drawSquareQ<span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">, </span>qVals<span style="font-weight: bold">, </span>minVal<span style="font-weight: bold">, </span>maxVal<span style="font-weight: bold">, </span>valStrs<span style="font-weight: bold">, </span>bestActions<span style="font-weight: bold">, </span>isCurrent<span style="font-weight: bold">):

  (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">) = </span>to_screen<span style="font-weight: bold">((</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">))
  
  </span>center <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">)
  </span>nw <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">-</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">-</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">)
  </span>ne <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">+</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">-</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">)
  </span>se <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">+</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">+</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">)
  </span>sw <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">-</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">+</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">)
  </span>n <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">-</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">+</span><span style="color: red">5</span><span style="font-weight: bold">)
  </span>s <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">+</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">)
  </span>w <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">-</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">+</span><span style="color: red">5</span><span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">)
  </span>e <span style="font-weight: bold">= (</span>screen_x<span style="font-weight: bold">+</span><span style="color: red">0.5</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">-</span><span style="color: red">5</span><span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">)
  
  </span>actions <span style="font-weight: bold">= </span>qVals<span style="font-weight: bold">.</span>keys<span style="font-weight: bold">()
  </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">:
    
    </span>wedge_color <span style="font-weight: bold">= </span>getColor<span style="font-weight: bold">(</span>qVals<span style="font-weight: bold">[</span>action<span style="font-weight: bold">], </span>minVal<span style="font-weight: bold">, </span>maxVal<span style="font-weight: bold">)

    </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'north'</span><span style="font-weight: bold">:
      </span>polygon<span style="font-weight: bold">( (</span>center<span style="font-weight: bold">, </span>nw<span style="font-weight: bold">, </span>ne<span style="font-weight: bold">), </span>wedge_color<span style="font-weight: bold">, </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
      </span><span style="color: green; font-style: italic">#text(n, text_color, valStr, "Courier", 8, "bold", "n")
    </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'south'</span><span style="font-weight: bold">:
      </span>polygon<span style="font-weight: bold">( (</span>center<span style="font-weight: bold">, </span>sw<span style="font-weight: bold">, </span>se<span style="font-weight: bold">), </span>wedge_color<span style="font-weight: bold">, </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
      </span><span style="color: green; font-style: italic">#text(s, text_color, valStr, "Courier", 8, "bold", "s")
    </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'east'</span><span style="font-weight: bold">:
      </span>polygon<span style="font-weight: bold">( (</span>center<span style="font-weight: bold">, </span>ne<span style="font-weight: bold">, </span>se<span style="font-weight: bold">), </span>wedge_color<span style="font-weight: bold">, </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
      </span><span style="color: green; font-style: italic">#text(e, text_color, valStr, "Courier", 8, "bold", "e")
    </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'west'</span><span style="font-weight: bold">:
      </span>polygon<span style="font-weight: bold">( (</span>center<span style="font-weight: bold">, </span>nw<span style="font-weight: bold">, </span>sw<span style="font-weight: bold">), </span>wedge_color<span style="font-weight: bold">, </span>filled <span style="font-weight: bold">= </span><span style="color: red">1</span><span style="font-weight: bold">, </span>smoothed <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
      </span><span style="color: green; font-style: italic">#text(w, text_color, valStr, "Courier", 8, "bold", "w")
      
  </span>square<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), 
                 </span><span style="color: red">0.5</span><span style="font-weight: bold">* </span>GRID_SIZE<span style="font-weight: bold">, 
                 </span>color <span style="font-weight: bold">= </span>EDGE_COLOR<span style="font-weight: bold">,
                 </span>filled <span style="font-weight: bold">= </span><span style="color: red">0</span><span style="font-weight: bold">,
                 </span>width <span style="font-weight: bold">= </span><span style="color: red">3</span><span style="font-weight: bold">)
  </span>line<span style="font-weight: bold">(</span>ne<span style="font-weight: bold">, </span>sw<span style="font-weight: bold">, </span>color <span style="font-weight: bold">= </span>EDGE_COLOR<span style="font-weight: bold">)
  </span>line<span style="font-weight: bold">(</span>nw<span style="font-weight: bold">, </span>se<span style="font-weight: bold">, </span>color <span style="font-weight: bold">= </span>EDGE_COLOR<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">if </span>isCurrent<span style="font-weight: bold">:
    </span>circle<span style="font-weight: bold">( (</span>screen_x<span style="font-weight: bold">, </span>screen_y<span style="font-weight: bold">), </span><span style="color: red">0.1</span><span style="font-weight: bold">*</span>GRID_SIZE<span style="font-weight: bold">, </span>LOCATION_COLOR<span style="font-weight: bold">, </span>fillColor<span style="font-weight: bold">=</span>LOCATION_COLOR <span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">:
    </span>text_color <span style="font-weight: bold">= </span>TEXT_COLOR
    <span style="color: blue; font-weight: bold">if </span>qVals<span style="font-weight: bold">[</span>action<span style="font-weight: bold">] &lt; </span>max<span style="font-weight: bold">(</span>qVals<span style="font-weight: bold">.</span>values<span style="font-weight: bold">()): </span>text_color <span style="font-weight: bold">= </span>MUTED_TEXT_COLOR
    valStr <span style="font-weight: bold">= </span><span style="color: red">""
    </span><span style="color: blue; font-weight: bold">if </span>action <span style="color: blue; font-weight: bold">in </span>valStrs<span style="font-weight: bold">:
      </span>valStr <span style="font-weight: bold">= </span>valStrs<span style="font-weight: bold">[</span>action<span style="font-weight: bold">]
    </span>h <span style="font-weight: bold">= -</span><span style="color: red">20
    </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'north'</span><span style="font-weight: bold">:
      </span><span style="color: green; font-style: italic">#polygon( (center, nw, ne), wedge_color, filled = 1, smooth = 0)
      </span>text<span style="font-weight: bold">(</span>n<span style="font-weight: bold">, </span>text_color<span style="font-weight: bold">, </span>valStr<span style="font-weight: bold">, </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, </span>h<span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"n"</span><span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'south'</span><span style="font-weight: bold">:
      </span><span style="color: green; font-style: italic">#polygon( (center, sw, se), wedge_color, filled = 1, smooth = 0)
      </span>text<span style="font-weight: bold">(</span>s<span style="font-weight: bold">, </span>text_color<span style="font-weight: bold">, </span>valStr<span style="font-weight: bold">, </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, </span>h<span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"s"</span><span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'east'</span><span style="font-weight: bold">:
      </span><span style="color: green; font-style: italic">#polygon( (center, ne, se), wedge_color, filled = 1, smooth = 0)
      </span>text<span style="font-weight: bold">(</span>e<span style="font-weight: bold">, </span>text_color<span style="font-weight: bold">, </span>valStr<span style="font-weight: bold">, </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, </span>h<span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"e"</span><span style="font-weight: bold">)
    </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'west'</span><span style="font-weight: bold">:
      </span><span style="color: green; font-style: italic">#polygon( (center, nw, sw), wedge_color, filled = 1, smooth = 0)
      </span>text<span style="font-weight: bold">(</span>w<span style="font-weight: bold">, </span>text_color<span style="font-weight: bold">, </span>valStr<span style="font-weight: bold">, </span><span style="color: red">"Courier"</span><span style="font-weight: bold">, </span>h<span style="font-weight: bold">, </span><span style="color: red">"bold"</span><span style="font-weight: bold">, </span><span style="color: red">"w"</span><span style="font-weight: bold">)


</span><span style="color: blue; font-weight: bold">def </span>getColor<span style="font-weight: bold">(</span>val<span style="font-weight: bold">, </span>minVal<span style="font-weight: bold">, </span>max<span style="font-weight: bold">):
  </span>r<span style="font-weight: bold">, </span>g <span style="font-weight: bold">= </span><span style="color: red">0.0</span><span style="font-weight: bold">, </span><span style="color: red">0.0
  </span><span style="color: blue; font-weight: bold">if </span>val <span style="font-weight: bold">&lt; </span><span style="color: red">0 </span><span style="color: blue; font-weight: bold">and </span>minVal <span style="font-weight: bold">&lt; </span><span style="color: red">0</span><span style="font-weight: bold">:
    </span>r <span style="font-weight: bold">= </span>val <span style="font-weight: bold">* </span><span style="color: red">0.65 </span><span style="font-weight: bold">/ </span>minVal
  <span style="color: blue; font-weight: bold">if </span>val <span style="font-weight: bold">&gt; </span><span style="color: red">0 </span><span style="color: blue; font-weight: bold">and </span>max <span style="font-weight: bold">&gt; </span><span style="color: red">0</span><span style="font-weight: bold">:
    </span>g <span style="font-weight: bold">= </span>val <span style="font-weight: bold">* </span><span style="color: red">0.65 </span><span style="font-weight: bold">/ </span>max
  <span style="color: blue; font-weight: bold">return </span>formatColor<span style="font-weight: bold">(</span>r<span style="font-weight: bold">,</span>g<span style="font-weight: bold">,</span><span style="color: red">0.0</span><span style="font-weight: bold">)


</span><span style="color: blue; font-weight: bold">def </span>square<span style="font-weight: bold">(</span>pos<span style="font-weight: bold">, </span>size<span style="font-weight: bold">, </span>color<span style="font-weight: bold">, </span>filled<span style="font-weight: bold">, </span>width<span style="font-weight: bold">):
  </span>x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">= </span>pos
  dx<span style="font-weight: bold">, </span>dy <span style="font-weight: bold">= </span>size<span style="font-weight: bold">, </span>size
  <span style="color: blue; font-weight: bold">return </span>polygon<span style="font-weight: bold">([(</span>x <span style="font-weight: bold">- </span>dx<span style="font-weight: bold">, </span>y <span style="font-weight: bold">- </span>dy<span style="font-weight: bold">), (</span>x <span style="font-weight: bold">- </span>dx<span style="font-weight: bold">, </span>y <span style="font-weight: bold">+ </span>dy<span style="font-weight: bold">), (</span>x <span style="font-weight: bold">+ </span>dx<span style="font-weight: bold">, </span>y <span style="font-weight: bold">+ </span>dy<span style="font-weight: bold">), (</span>x <span style="font-weight: bold">+ </span>dx<span style="font-weight: bold">, </span>y <span style="font-weight: bold">- </span>dy<span style="font-weight: bold">)], </span>outlineColor<span style="font-weight: bold">=</span>color<span style="font-weight: bold">, </span>fillColor<span style="font-weight: bold">=</span>color<span style="font-weight: bold">, </span>filled<span style="font-weight: bold">=</span>filled<span style="font-weight: bold">, </span>width<span style="font-weight: bold">=</span>width<span style="font-weight: bold">, </span>smoothed<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
  
  
</span><span style="color: blue; font-weight: bold">def </span>to_screen<span style="font-weight: bold">(</span>point<span style="font-weight: bold">):
  ( </span>gamex<span style="font-weight: bold">, </span>gamey <span style="font-weight: bold">) = </span>point
  x <span style="font-weight: bold">= </span>gamex<span style="font-weight: bold">*</span>GRID_SIZE <span style="font-weight: bold">+ </span>MARGIN  
  y <span style="font-weight: bold">= (</span>GRID_HEIGHT <span style="font-weight: bold">- </span>gamey <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">)*</span>GRID_SIZE <span style="font-weight: bold">+ </span>MARGIN  
  <span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">( </span>x<span style="font-weight: bold">, </span>y <span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>to_grid<span style="font-weight: bold">(</span>point<span style="font-weight: bold">):
  (</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">) = </span>point
  x <span style="font-weight: bold">= </span>int <span style="font-weight: bold">((</span>y <span style="font-weight: bold">- </span>MARGIN <span style="font-weight: bold">+ </span>GRID_SIZE <span style="font-weight: bold">* </span><span style="color: red">0.5</span><span style="font-weight: bold">) / </span>GRID_SIZE<span style="font-weight: bold">)
  </span>y <span style="font-weight: bold">= </span>int <span style="font-weight: bold">((</span>x <span style="font-weight: bold">- </span>MARGIN <span style="font-weight: bold">+ </span>GRID_SIZE <span style="font-weight: bold">* </span><span style="color: red">0.5</span><span style="font-weight: bold">) / </span>GRID_SIZE<span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">print </span>point<span style="font-weight: bold">, </span><span style="color: red">"--&gt;"</span><span style="font-weight: bold">, (</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">)
  </span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">(</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">)
</span>
  </pre>
  </body>
  </html>
  